﻿using B.S.BasicData.Api.Write.Application.Command.Role;
using B.S.BasicData.Domain;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interface;
using MediatR;
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;

namespace B.S.BasicData.Api.Write.Application.Handler.Role
{
    public class DelUserRoleHandler : IRequestHandler<DelUserRoleCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<UserRoleModel> userRoleRepo;

        public DelUserRoleHandler(IBaseRepository<UserRoleModel> userRoleRepo)
        {
            this.userRoleRepo = userRoleRepo;
        }
        public Task<ApiResult<int>> Handle(DelUserRoleCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> res = new ApiResult<int>();
            var info = userRoleRepo.GetAll().Where(x => x.UserId == request.UserId && x.RoleId == request.RoleId).FirstOrDefault();
            var account = userRoleRepo.Del(info);
            if (account>0)
            {
                res.Data= account;
                res.Code = ApiEnum.Success;
                res.Msg = "删除成功";
            }
            else
            {
                res.Data = account;
                res.Code = ApiEnum.Error;
                res.Msg = "删除失败";
            }
            return Task.FromResult(res);
        }
    }
}
